package com.eoner.managerlibrary.user.dispatch;

import android.text.TextUtils;

import com.eoner.baselibrary.bean.personal.CustomerOrderInfo;
import com.eoner.commonbean.user.CustomerBean;
import com.eoner.common.config.Config;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.managerlibrary.user.annotation.UpdateAllUserCache;
import com.eoner.managerlibrary.user.cache.UserDataCache;
import com.google.gson.Gson;

public class UserDataStorage {

    //用户信息key
    private final static String CUSTOMER_SP = "customer_sp2";
    private final static String CUSTOMERSTATUS = "customer_status";

    //用户id
    private String customerId;
    //用户token
    private String customerToken;
    //用户信息 (登录之后单独接口获取)
    private UserDataCache userInfo;

    //用户邀请请码（这个比较特别，因为是在注册之前）
    private String inviteCode;

    /**
     * 自动登陆
     * 如果存在用户缓存，默认记录登陆信息
     * */
    public void autoLogin() {

        customerId = SPUtils.getInstance().getString(Config.CUSTOMERID);
        customerToken = SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN);
        String sp_customer = SPUtils.getInstance().getString(CUSTOMER_SP);
        if (!TextUtils.isEmpty(sp_customer)) {
            userInfo = new Gson().fromJson(SPUtils.getInstance().getString(CUSTOMER_SP), UserDataCache.class);
        }
    }

    /**
     * 是否登录
     * */
    public boolean isLogin() {
        if(!TextUtils.isEmpty(SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN))){
            return true;
        }
        return false;
    }

    /**
    * 更新用户状态
    * */
    public void updateUserStatus(boolean isDisable) {
        SPUtils.getInstance().put(CUSTOMERSTATUS, isDisable);
    }

    /**
     * 更新用户状态
     * */
    public void updateUserStatus(String isDisable) {
        if ("1".equals(isDisable) || "true".equals(isDisable)) {
            SPUtils.getInstance().put(CUSTOMERSTATUS, true);
        } else {
            SPUtils.getInstance().put(CUSTOMERSTATUS, false);
        }
    }

    /**
     * 更新用户信息
     * */
    @UpdateAllUserCache
    public void updateUserInfo(CustomerBean.DataBean dataBean) {
        if (userInfo == null) {
            //保存内存
            userInfo = UserDataCache.transformByCustomInfo(dataBean);
        } else {
            userInfo.assignByCustomInfo(dataBean);
        }
        //保存sp
        SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
    }

    /**
     * 更新用户信息
     * */
    @UpdateAllUserCache
    public void updateUserInfo(CustomerOrderInfo.DataBean dataBean) {
        if (userInfo == null) {
            //保存内存
            userInfo = UserDataCache.transformByCustomOrder(dataBean);
        } else {
            userInfo.assignByCustomOrderInfo(dataBean);
        }
        //保存sp
        SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
    }

    /**
    * 更改手机号
    * */
    public void updateUserMobile(String mobile) {
        if (userInfo != null) {
            //更改手机号
            userInfo.setMobile(mobile);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }

    /**
    * 更改身份证真实姓名
    * */
    public void updateUserIdCardRealName(String realName) {
        if (userInfo != null) {
            //更改真实姓名
            userInfo.setIdCardRealName(realName);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }

    /**
     * 更改身份证号
     * */
    public void updateUserIdCardNumber(String idCardNumber) {
        if (userInfo != null) {
            //更改身份证号
            userInfo.setIdCardIdNumber(idCardNumber);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }

    /**
    * 更改是否是代理商
    * */
    public void updateUserIsAgent(boolean isAgent) {
        if (userInfo != null) {
            //更改是否是代理商
            userInfo.setAgent(isAgent);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }

    /**
    * 更改用户等级
    * */
    public void updateUserLevel(String userLevel) {
        if (userInfo != null) {
            //更改身份证号
            userInfo.setRoleLevel(userLevel);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }

    /**
     * 更改售后地址是否填写
     * */
    public void updateCheckAddress(boolean checkAddress) {
        if (userInfo != null) {
            //更改邀请码
            userInfo.setCheck_refund_address(checkAddress);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        }
    }


    /**
    * 更改邀请码
    * */
    public void updateInviteCode(String inviteCode) {
        if (userInfo != null) {
            //更改邀请码
            userInfo.setInviteCode(inviteCode);
            //保存sp
            SPUtils.getInstance().put(CUSTOMER_SP, new Gson().toJson(userInfo));
        } else {
            this.inviteCode = inviteCode;
        }
    }


    /**
     * 获取用户id
     * */
    public String getCustomerId() {
        if (TextUtils.isEmpty(customerId)) {
            customerId = SPUtils.getInstance().getString(Config.CUSTOMERID);
        }
        return customerId;
    }

    /**
     * 获取用户token
     * */
    public String getCustomerToken() {
        if (TextUtils.isEmpty(customerToken)) {
            customerToken = SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN);
        }
        return customerToken;
    }

    /**
     * 获取用户缓存信息
     * */
    public UserDataCache getUserInfo() {
        if (userInfo != null && userInfo.getMobile() != null) {
            //直接返回用户信息
            return userInfo;
        } else {
            //再去sp读取
            String sp_customer = SPUtils.getInstance().getString(CUSTOMER_SP);
            if(!TextUtils.isEmpty(sp_customer)){
                userInfo = new Gson().fromJson(SPUtils.getInstance().getString(CUSTOMER_SP), UserDataCache.class);
            }
        }
        return userInfo;
    }

    /**
    * 用户状态
    * */
    public boolean isUserDisable() {

        String isDisableStr = "";
        boolean isDisable = false;
        try {
            isDisableStr = SPUtils.getInstance().getString(CUSTOMERSTATUS);
        } catch (Exception e){
            e.printStackTrace();
        }
        try {
            isDisable = SPUtils.getInstance().getBoolean(CUSTOMERSTATUS);
        } catch (Exception e){
            e.printStackTrace();
        }
        if ("1".equals(isDisableStr) || "true".equals(isDisableStr) || isDisable) {
            return true;
        }
        return false;
    }

    /**
     * 头像
     * */
    public String getHeadImgUrl() {
        if (getUserInfo() != null) {
            return userInfo.getHeadImgUrl();
        }
        return "";
    }

    /**
     * 昵称
     * */
    public String getNickName() {
        if (getUserInfo() != null) {
            return userInfo.getNickName();
        }
        return "";
    }

    /**
     * 昵称
     * */
    public String getLevel_bg() {
        if (getUserInfo() != null) {
            return userInfo.getLevel_bg();
        }
        return "";
    }

    /**
     * 获取手机号
     * */
    public String getMobile() {
        if (getUserInfo() != null) {
            return userInfo.getMobile();
        }
        return "";
    }

    public String getIdCardNo() {
        if (getUserInfo() != null) {
            return userInfo.getIdCardIdNumber();
        }
        return "";
    }

    public String getIdCardRealName() {
        if (getUserInfo() != null) {
            return userInfo.getIdCardRealName();
        }
        return "";
    }

    //获取身份提示信息
    public String getIdCardMessage() {
        if (getUserInfo() != null) {
            return userInfo.getCheckIdCardMessage();
        }
        return "";
    }

    //获取是否代理商
    public boolean isAgent() {
        if (getUserInfo() != null) {
            return userInfo.isAgent();
        }
        return false;
    }

    //用户等级
    public String userRoleLevel() {
        if (getUserInfo() != null) {
            return userInfo.getRoleLevel();
        }
        return "0";
    }

    //邀请码
    public String inviteCode() {
        if (getUserInfo() != null) {
            return userInfo.getInviteCode();
        }
        if (!TextUtils.isEmpty(inviteCode)) {
            return inviteCode;
        }
        return "";
    }

    /**
    * 是否实名认证
    * */
    public String getCheckIdCardStatus(){
        if (getUserInfo() != null) {
            return userInfo.getCheckIdCardStatus();
        }
        return "";
    }

    public boolean getCheckRefundAddress() {
        if (getUserInfo() != null) {
            return userInfo.getCheck_refund_address();
        }
        return false;
    }

    /**
     * 清除登陆信息
     * */
    public void clearLoginInfo() {
        //用户id
        customerId = "";
        //用户token
        customerToken = "";
        //用户信息 (登录之后单独接口获取)
        userInfo = null;
        //TODO:清除sp
        SPUtils.getInstance().remove(Config.CUSTOMERID);
        SPUtils.getInstance().remove(Config.CUSTOMER_TOKEN);
        SPUtils.getInstance().remove(CUSTOMER_SP);
    }

}
